//页面加载完毕
$(function () {

    var studentLossItem_datagrid = $("#studentLossItem_datagrid");
    var studentLossItem_dialog = $("#studentLossItem_dialog");
    var studentLossItem_form = $("#studentLossItem_form");

    //学员流失列表初始化
    studentLossItem_datagrid.datagrid({
        url:"/studentLossItem/list.do",
        fit:true,
        fitColumns:true,
        striped:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        toolbar:"#tb",
        columns:[[
            {field:"id", hidden:true},
            {field:"name", title:"学员姓名", width:80,align:'center'},
            {field:"qqNum", title:"QQ", width:80,align:'center'},
            {field:"tel", title:"电话", width:80,align:'center'},
            {field:"classDays", title:"上课天数", width:80,align:'center'},
            {field:"lossClass", title:"流失班级", width:80,align:'center',formatter:lossClass},
            {field:"lossStage", title:"流失阶段", width:80,align:'center',formatter:lossStage},
            {field:"lossReason", title:"流失原因", width:80,align:'center'},
            {field:"lossTime", title:"流失时间", width:80,align:'center'},
            {field:"responsible", title:"经办人", width:80,align:'center',formatter:responsible},
            {field:"marketingPerson", title:"营销人员", width:80,align:'center',formatter:marketingPersonA},
            {field:"entryUser", title:"录入人", width:80,align:'center,',formatter:entryUser},
            {field:"refundAmount", title:"退款金额", width:80,align:'center'},
            {field:"whetherRefund", title:"是否退款", width:80,formatter:whetherRefund,align:'center'},
            {field:"auditState", title:"审核状态", width:80,formatter:auditStateB,align:'center'},
            {field:"auditUser", title:"审核人", width:80,formatter:auditUser,align:'center'}
        ]]
    });
    //end

    var cmdObj = {
        //学员流失查看
        lookView : function (){
            //判断是否选中
            var row = studentLossItem_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条查看数据", "info");
                return;
            }
            console.log(row);

            //添加对话框打开
            studentLossItem_dialog.dialog("open");
            //修改对话框标题
            studentLossItem_dialog.dialog("setTitle","学员流失信息");

            //营销人员
            if(row.marketingPerson){
                row["marketingPerson.id"] = row.marketingPerson.id;
            }
            //流失班级
            if(row.lossClass){
                row["lossClass.id"] = row.lossClass.id;
            }
            //流失阶段
            if(row.lossStage){
                row["lossStage.id"] = row.lossStage.id;
            }
            //经办人
            if(row.responsible){
                row["responsible.id"] = row.responsible.id;
            }
            //录入人
            if(row.entryUser){
                row["entryUser.id"] = row.entryUser.id;
            }

            //数据回显
            studentLossItem_form.form("load",row);
            //隐藏保存按钮
            $('.dialog-button a:eq(0)').hide();
            //设置只读
            $(".readOn").textbox('readonly',true);
        },
        //编辑操作
        edit : function () {
            //判断是否选中
            var row = studentLossItem_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条编辑数据", "info");
                return;
            }
            if(row.auditState == 1){
                $.messager.alert('温馨提示',"已审核!,不允许编辑!", "info");
                return;
            }

            //添加/编辑对话框打开
            studentLossItem_dialog.dialog("open");
            //修改对话框标题
            studentLossItem_dialog.dialog("setTitle","学员流失编辑");

            //营销人员
            if(row.marketingPerson){
                row["marketingPerson.id"] = row.marketingPerson.id;
            }
            //流失班级
            if(row.lossClass){
                row["lossClass.id"] = row.lossClass.id;
            }
            //流失阶段
            if(row.lossStage){
                row["lossStage.id"] = row.lossStage.id;
            }
            //经办人
            if(row.responsible){
                row["responsible.id"] = row.responsible.id;
            }
            //录入人
            if(row.entryUser){
                row["entryUser.id"] = row.entryUser.id;
            }
            //数据回显
            studentLossItem_form.form("load",row);
            //取消只读
            $(".readOn").textbox('readonly',false);
            //取消隐藏按钮
            $('.dialog-button a:eq(0)').show();

        },
        //审核
        audit : function () {
            //判断是否选中
            var row = studentLossItem_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条数据", "info");
                return;
            }
            if(row.auditState == 1){
                $.messager.alert('温馨提示',"已审核!", "info");
                return;
            }
            $.messager.confirm('温馨提示','是否确认审核?',function(ret){
                if (ret){
                    //确定审核
                    $.get("/studentLossItem/audit.do?id=" + row.id,function (data) {
                        if(data.success){
                            //审核成功
                            $.messager.confirm('温馨提示','审核成功!',function(ret){
                                //刷新数据
                                studentLossItem_datagrid.datagrid("reload");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //流失学员更新
        save : function (){
            //表单提交url
            var url = "";
            //使用id区分保存于更新操作
            var id = $("#studentLossItemId").val();
            if(id){
                //编辑
                url = "/studentLossItem/update.do";
            }else{
               return;
            }
            //提交表单
            studentLossItem_form.form("submit", {
                url:url,
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        //提示成功
                        $.messager.confirm('温馨提示',"操作成功",function(r){
                            //关闭窗口
                            studentLossItem_dialog.dialog("close");
                            //刷新数据
                            studentLossItem_datagrid.datagrid("reload");
                        });
                    }else{
                        $.messager.alert('温馨提示',data.msg, "error");
                        //关闭窗口
                        studentLossItem_dialog.dialog("close");
                        //刷新数据
                        studentLossItem_datagrid.datagrid("reload");
                    }
                }

            })
        },
        //高级查询  思路: 加载学员流失列表时  将多条件查询数据一同发起请求
        searchT : function () {
            var keyword = $("#keyword").val();
            var clazzId = $("#clazzId").val();
            var beginDate = $("#beginDate").val();
            var endDate = $("#endDate").val();
            //加载数据列表时 携带额外参数
            studentLossItem_datagrid.datagrid("load",{
                keyword:keyword ,
                clazzId:clazzId ,
                beginDate:beginDate ,
                endDate:endDate
            })
        },
        //取消操作
        cancel : function (){
            studentLossItem_dialog.dialog("close");
        },
        //刷新
        reload : function (){
            studentLossItem_datagrid.datagrid("reload");
        }
    };

    //调用
    //给所有的按钮统一绑定事件
    $("a[data-cmd]").on("click",function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    //对话框初始化
    studentLossItem_dialog.dialog({
        width: "50%",
        height: "60%",
        buttons:"#bb",
        closable:true,
        closed:true
    });
    //end

});

//是否退款格式化
function whetherRefund(value, row, index){
    if(value == 0){
        return "<span style='color: red'>未退款</span>"
    }
    return "<span style='color: #00ee00;'>已退款</span>";
}
//审核状态格式化
function auditStateB(value, row, index){
    if(value == 0){
        return "<span style='color: red'>未审核</span>"
    }
    return "<span style='color: #00ee00;'>已审核</span>";
}
//审核人格式化
function auditUser(value, row, index){
    return value ? value.username : "";
}
//经办人格式化
function responsible(value, row, index){
    return value ? value.username : "";
}
//营销人员格式化
function marketingPersonA(value, row, index){
    return value ? value.username : "";
}
//录入人格式化
function entryUser(value, row, index){
    return value ? value.username : "";
}

//流失班级格式化
function lossClass(value, row, index){
    return value ? value.name : "";
}

//流失阶段格式化
function lossStage(value, row, index){
    return value ? value.name : "";
}
